// pages/cssCalculator.js
Page({
  // onShareAppMessage() {
  //   return {
  //     title: 'swiper',
  //     path: 'page/component/pages/swiper/swiper'
  //   }
  // },

  data: {
    vertical: false,
    current: 0,//0为恒速，1为多剂量
    k0:0,
    t1_2:0,
    ke: 0,
    vd: 0,
    items: [
      {value: 'g/min', name: 'g/min', checked: 'true'},
      {value: 'mg/min', name: 'mg/min'},
      {value: 'U/min', name: 'U/min'},
    ],
    k0_unit:'g/min',
    unit:'g/L',
    // 多剂量给药方式
    items2: [
      {value: '0', name: '口服', checked: 'true'},
      {value: '1', name: '静脉'},
    ],
    F:0,
    D:0,
    items3: [
      {value: 'g', name: 'g', checked: 'true'},
      {value: 'mg', name: 'mg'},
    ],
    h: 0,
    t1_22: 0,
    ke2: 0,
    v_d2: 0,
    unit2:"g/L",
    D_unit:'g',

    show: false,
    duration: 300,
    position: 'right',
    round: false,
    overlay: true,
    customStyle: '',
    overlayStyle: ''
  },
  principle() {
    wx.navigateTo({
      url: '../principle/principle?mean='+this.data.current,
    })
  },

  tip() {
    wx.navigateTo({
      url: '../vdDescribe/vdDescribe',
    })
  },
  check2() {
    if (Number(this.data.h) === 0) {
      wx.showToast({
        title: '给药间隔需>0',
        icon:'error'
      })
      return false;
    }
    if (Number(this.data.t1_22) === 0) {
      wx.showToast({
        title: '半衰期需>0',
        icon:'error'
      })
      return false;
    }
    if (Number(this.data.v_d2) === 0) {
      wx.showToast({
        title: '分布容积需>0',
        icon: 'error'
      })
      return false;
    }
    return true;
  },
  calculate2() {
    let css;
    if(!this.check2()) {
      return ;
    }
    if (this.data.items2[1].checked) {
      css = Number((1 * this.data.D) / (this.data.ke2 * this.data.v_d2 * this.data.h) ).toFixed(2) ;
    } else {
      css = Number((this.data.F / 100 * this.data.D) / (this.data.ke2 * this.data.v_d2 * this.data.h) ).toFixed(2) ;
    }
    const _this = this;
      wx.showModal({
        title: '计算结果',
        content: `${css.toString()} （${this.data.unit2}）`,
        confirmText: '复制结果',
        success (res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: `${css.toString()} （${_this.data.unit2}）`,
              success (res) {
                wx.getClipboardData({
                  success (res) {
                    console.log(res.data) // data
                  }
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          }
      })
  },
  bindv_d2(e) {
    this.setData({
      v_d2: e.detail.value
    })
  },
  bindt1_22(e) {
    this.setData({
      t1_22: e.detail.value
    })
    this.setData({
      ke2:0.693/e.detail.value
    })
  },
  bindh(e){
    this.setData({
      h: e.detail.value
    })
  },
  bindF(e){
    this.setData({
      F: e.detail.value
    })
  },
  bindD(e){
    this.setData({
      D: e.detail.value
    })
  },
  radioChange3(e) {
    const unitmap = new Map([['g','g/L'], ['mg','mg/L']]);
    this.setData({
      unit2:unitmap.get(e.detail.value),
      D_unit: e.detail.value
    })
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const items3 = this.data.items3
    for (let i = 0, len = items3.length; i < len; ++i) {
      items3[i].checked = items3[i].value === e.detail.value
    }
    this.setData({
      items3
    })
  },
  radioChange2(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const items2 = this.data.items2
    for (let i = 0, len = items2.length; i < len; ++i) {
      items2[i].checked = items2[i].value === e.detail.value
    }

    this.setData({
      items2
    })
  
  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const unitmap = new Map([['g/min','g/L'], ['mg/min','mg/L'], ['u/min', 'u/L']]);
    this.setData({
      unit:unitmap.get(e.detail.value),
      k0_unit: e.detail.value
    })
    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }

    this.setData({
      items
    })
  },

  check() {
    console.log(this.data.t1_2)
    if (Number(this.data.t1_2) === 0) {
      wx.showToast({
        title: '半衰期需>0',
        icon:'error'
      })
      return false;
    }
    if (Number(this.data.vd) === 0) {
      wx.showToast({
        title: '分布容积需>0',
        icon: 'error'
      })
      return false;
    }
    return true;
  },

  calculate() {
    if (this.check()) {
      let css = Number((this.data.k0*60)/(this.data.ke *this.data.vd)).toFixed(2);
      const _this = this;
      wx.showModal({
        title: '计算结果',
        content: `${css.toString()} （${this.data.unit}）`,
        confirmText: '复制结果',
        success (res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: `${css.toString()} （${_this.data.unit}）`,
              success (res) {
                wx.getClipboardData({
                  success (res) {
                    console.log(res.data) // data
                  }
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          }
      })
    }
    
  },

  bindv_d(e) {
    this.setData({
      vd: e.detail.value
    })
  },

  bindk_e(e) {
    this.setData({
      ke: e.detail.value
    })
  },

  bindt1_2(e) {
    this.setData({
      t1_2: e.detail.value
    });
    this.setData({
      ke:0.693/e.detail.value
    })
  },

  bindk0(e) {
    this.setData({
      k0: e.detail.value
    })
  },

  bindchange(e) {
    this.setData({
      current:e.detail.current
    })
  },
  
  change(e) {
    this.setData({
      current:e.target.dataset.page
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})